package com.ruoyi.supplier.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.supplier.domain.ProductSpecRel;
import com.ruoyi.supplier.domain.bo.ProductSpecRelBo;
import com.ruoyi.supplier.domain.vo.ProductSpecRelVo;
import com.ruoyi.supplier.service.IProductSpecRelService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.supplier.domain.bo.ProductSpecBo;
import com.ruoyi.supplier.domain.vo.ProductSpecVo;
import com.ruoyi.supplier.domain.ProductSpec;
import com.ruoyi.supplier.mapper.ProductSpecMapper;
import com.ruoyi.supplier.service.IProductSpecService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 商品规格组记录Service业务层处理
 *
 * @author ruoyi
 * @date 2023-09-18
 */
@RequiredArgsConstructor
@Service
@DS("slave")
public class ProductSpecServiceImpl implements IProductSpecService {

    private final ProductSpecMapper baseMapper;

    @Autowired
    private IProductSpecRelService productSpecRelService;

    /**
     * 查询商品规格组记录
     */
    @Override
    public ProductSpecVo queryById(Integer specId){
        return baseMapper.selectVoById(specId);
    }

    /**
     * 查询商品规格组记录列表
     */
    @Override
    public TableDataInfo<ProductSpecVo> queryPageList(ProductSpecBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ProductSpec> lqw = buildQueryWrapper(bo);
        Page<ProductSpecVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询商品规格组记录列表
     */
    @Override
    public List<ProductSpecVo> queryList(ProductSpecBo bo) {
        LambdaQueryWrapper<ProductSpec> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ProductSpec> buildQueryWrapper(ProductSpecBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ProductSpec> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getSpecName()), ProductSpec::getSpecName, bo.getSpecName());
        lqw.eq(bo.getMerchantId() != null, ProductSpec::getMerchantId, bo.getMerchantId());
        lqw.eq(bo.getSupplierId() != null, ProductSpec::getSupplierId, bo.getSupplierId());
        return lqw;
    }

    /**
     * 新增商品规格组记录
     */
    @Override
    public Boolean insertByBo(ProductSpecBo bo) {
        ProductSpec add = BeanUtil.toBean(bo, ProductSpec.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setSpecId(add.getSpecId());
        }
        return flag;
    }

    /**
     * 修改商品规格组记录
     */
    @Override
    public Boolean updateByBo(ProductSpecBo bo) {
        ProductSpec update = BeanUtil.toBean(bo, ProductSpec.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ProductSpec entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除商品规格组记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Integer> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public ProductSpecVo queryByProductId(Integer productId) {
        ProductSpecRelBo specRelVo = new ProductSpecRelBo();
        specRelVo.setProductId(productId);
        List<ProductSpecRelVo> productSpecRelVos = productSpecRelService.queryList(specRelVo);
        if(CollectionUtil.isEmpty(productSpecRelVos)){
            return null;
        }
        ProductSpecBo bo = new ProductSpecBo();
        bo.setSpecId(productSpecRelVos.stream().findFirst().get().getSpecId());
        List<ProductSpecVo> productSpecVos = this.queryList(bo);
        if(CollectionUtil.isEmpty(productSpecVos)){
            return null;
        }

        return productSpecVos.stream().findFirst().get();
    }
}
